<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Http\Requests\ExperConsAdd;
use App\Http\Requests\ExperReplyUser;
use App\Models\ExpertRecords;
use App\Models\ExpertSymptom;
use App\Models\ExpertCons;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class PatientController extends Controller
{
    // 专家问诊列表
    public function index()
    {
        $result = ExpertCons::whereUserId(auth('wechat')->id())
            ->orderByDesc('id')
            ->paginate($this->limit);
        return $this->writeJson(0, $result->items());
    }

    // 专家问诊详情
    public function detail(Request $request)
    {
        $id = $request->post('id');
        if (empty($id)) {
            return $this->writeJson(500, null, '参数错误');
        }

        $expertCons = ExpertCons::whereUserId(auth('wechat')->id())
            ->where('id', $id)
            ->first();
        if (empty($expertCons)) {
            return $this->writeJson(500, null, '数据不存在');
        }

        return $this->writeJson(0, $expertCons);
    }

    // 常见症状列表
    public function symptom()
    {
        $list = ExpertSymptom::orderByDesc('sorts')->get();
        return $this->writeJson(0, $list);
    }

    // 专家问诊表单提交
    public function add(ExperConsAdd $request)
    {
        $model = new ExpertCons();
        $model->setRawAttributes($request->validated());
        $model->user_id = auth('wechat')->id();
        $model->status = 1;
        $model->save();

        return $this->writeJson(0, null, '提交成功');
    }

    // 回复专家
    public function reply(ExperReplyUser $request)
    {
        $expertCons = ExpertCons::whereUserId(auth('wechat')->id())
            ->where('id', $request->post('expert_cons_id'))
            ->first();
        if (empty($expertCons)) {
            return $this->writeJson(500, null, '数据不存在');
        }
        if ($expertCons->status == 0) {
            return $this->writeJson(500, null, '已结束，无法再回复');
        }

        try {
            DB::beginTransaction();
            $model = new ExpertRecords();
            $model->user_id = auth('wechat')->id();
            $model->expert_cons_id = $request->post('expert_cons_id');
            $model->content = $request->post('content');
            $model->save();

            $expertCons->status = 1;
            $expertCons->save();

            DB::commit();
            return $this->writeJson(0, null, '回复成功');
        } catch (\Exception $exception) {
            DB::rollBack();
            \Log::error($exception);
        } catch (\Throwable $e) {
            DB::rollBack();
            \Log::error($e);
        }
        return $this->writeJson(500, null, '回复失败');
    }

    // 结束问诊
    public function end(Request $request)
    {
        $id = $request->post('id');
        if (empty($id)) {
            return $this->writeJson(500, null, '参数错误');
        }

        $expertCons = ExpertCons::whereUserId(auth('wechat')->id())
            ->where('id', $id)
            ->first();
        if (empty($expertCons)) {
            return $this->writeJson(500, null, '数据不存在');
        }
        if ($expertCons->status == 0) {
            return $this->writeJson(500, null, '请勿重复结束问诊');
        }
        $expertCons->status = 0;
        $expertCons->save();

        return $this->writeJson(0, null, '结束成功');
    }
}
